package cn.tude.xctf.boot21.controller;



import cn.tude.xctf.boot21.entity.Product;
import cn.tude.xctf.boot21.mapper.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

//@RestController 相当于controller注解的细化，在每一个方法上都加了@ResponseBody注解
@RestController
public class ProductController {
    @Autowired
    ProductMapper mapper;

    @RequestMapping("/insert")
    public String insert(Product product){
        System.out.println("product"+product);
        mapper.insert(product);
//        mapper.insert(product);
        return "添加成功";
    }




    @RequestMapping("/select")
    public String select(){
        List<Product> list = mapper.select();
        String html="<table border='2'>";
        html +="<caption>商品列表</caption>";
        html +="<tr><th>id</th><th>标题</th><th>价格</th><th>库存</th><th>操作</th></tr>";
        for (Product p:list){
            html +="<tr>";
            html +="<td>"+p.getId()+"</td>";
            html +="<td>"+p.getTitle()+"</td>";
            html +="<td>"+p.getPrice()+"</td>";
            html +="<td>"+p.getNum()+"</td>";
            html +="<td><a href='/delete?id="+p.getId()+"'>删除</a></th>";
            html +="<td><a href='/update?id="+p.getId()+"'>修改</a></th>";

            html +="</tr>";
        }
        html +="</table>";

        return html;

    }
    @RequestMapping("/delete" )
    public String delete(int id){
        mapper.deleById(id);
        return "删除成功! <a href='/select'>返回列表页面</a>";
    }

    @RequestMapping("/update" )
    public String update(Product product){
        mapper.update(product);
        return "修改成功! <a href='/select'>返回列表页面</a>";
    }



}

